package managedbeans;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import utils.dataaccess.DBInterface;
import utils.MailBean;
import utils.security.SHAHashUtil;
import entities.Authorisation;
import langresources.ResourcesController;

@ManagedBean(name="authorizationBean")
@SessionScoped
public class AuthorizationBean extends GenericForumBean{

    @EJB MailBean    mailBean;
    @EJB SHAHashUtil hasher;

    private String username;
    private String password;
    private String email;
    
    private Authorisation user;

    public AuthorizationBean() {
    }

    public String authorise(){
        user = lookupUser();
        username="";
        password="";
        if(user!=null){
            DBInterface.updateEventCounter();
            DBInterface.increaseUserCounter();
            return "ADMINISTRATOR_VIEW";
        } else {
            showMessage(ResourcesController.extractResource("MESSAGE.ACCESS_DENIED"), ResourcesController.extractResource("INVALID_REQUISITORY"));
            user = null;
        }
        return "USER_VIEW";
    }

    private Authorisation lookupUser(){
        Authorisation authEntry = utils.dataaccess.DBInterface.getEntry(username, password);
        return authEntry;
    }

    public void sendUserInfo(String username, String userpass){
        String to = email;
        String subject = ResourcesController.extractResource("PASSWORD_RESTORATION");
        String text = "Вы запросили восстановление пароля для ресурса"
                    + "JForum.com: \nЛогин: "+username+"\nПароль: "+userpass
                    + "\nС уважением, администрация JForum.ORG";
        mailBean.sendMessage(to,subject,text);
    }

    public void restorePassword(String email){
        Authorisation authEntry = DBInterface.getByEmail(email);
        if(authEntry != null) {
             sendUserInfo(authEntry.getUsername(), authEntry.getPassword());
             showMessage(ResourcesController.extractResource("MESSAGE.INFO"), ResourcesController.extractResource("REQUISITORY_SENT_TO")+" "+email);
             DBInterface.updateEventCounter();
        } else {
             showMessage(ResourcesController.extractResource("MESSAGE.INFO"), ResourcesController.extractResource("PASSWORD_NOT_FOUND"));
        }
    }
    
    public String logout(){
        user = null;
        return "LOGOUT";
    }


    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = hasher.getHash(password);
    }

    public String getUsername() {
        if(user == null){
            return ResourcesController.extractResource("GUEST");
        }
        return user.getUsername();
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Authorisation getUser() {
        return user;
    }

    public void setUser(Authorisation user) {
        this.user = user;
    }


}
